/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package todotask.core;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/*
Copyright (c) 2005, Corey Goldberg

StopWatch.java is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 * 
 * @ dakota
 * Modified: Added two methods to get the String in the format HH:MM:SS and a method to set a time Initial added
 * to the timeelapsed final.
 * 
 */
public class StopWatch {

    private long startTime = 0;
    private long stopTime = 0;
    private boolean running = false;
    private long timeInitial = 0;
    protected String timecont = "";
    public static final String PROP_TIMECONT = "timecont";
    Thread th = new Thread(new Runnable() {

        @Override
        public void run() {
            while (true) {
                try {
                    if (running) {
                        stopTime = System.currentTimeMillis();
                        setTimecont(getElapsedStringTime());
                    }
                    Thread.sleep(1000);
                } catch (Exception ex) {
                    //
                }
            }
        }
    });

    /**
     * Get the value of timecont
     *
     * @return the value of timecont
     */
    public String getTimecont() {
        return timecont;
    }

    /**
     * Set the value of timecont
     *
     * @param timecont new value of timecont
     */
    public void setTimecont(String timecont) {
        String oldTimecont = String.valueOf(getElapsedTimeSecs());
        this.timecont = timecont;
        propertyChangeSupport.firePropertyChange(PROP_TIMECONT, oldTimecont, timecont);
    }
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    public void start() {
        if (!th.isAlive()) {
            th.start();
        }
        System.out.println("Start: " + getElapsedStringTime());
        this.startTime = System.currentTimeMillis();
        this.running = true;
    }

    public void stop() {
        System.out.println("Stop: " + getElapsedStringTime());
        this.stopTime = System.currentTimeMillis();
        this.running = false;
        timeInitial = this.getElapsedTimeSecs();
    }

    //elaspsed time in milliseconds
    public long getElapsedTimeMilliSeconds() {
        long elapsed;
        if (running) {
            elapsed = (System.currentTimeMillis() - startTime);
        } else {
            elapsed = (stopTime - startTime);
        }
        elapsed += (this.timeInitial * 1000);
        return elapsed;
    }

    /**
     * Return time in HH:MM:SS format as String
     * @return 
     */
    public String getElapsedStringTime() {
        long elapsed;
        if (running) {
            elapsed = ((System.currentTimeMillis() - startTime) / 1000);
        } else {
            elapsed = ((stopTime - startTime) / 1000);
        }
        elapsed += this.timeInitial;
        long hours = elapsed / 3600;
        long remainder = elapsed % 3600;
        long minutes = remainder / 60;
        long seconds = remainder % 60;

        return ((hours < 10 ? "0" : "") + hours
                + ":" + (minutes < 10 ? "0" : "") + minutes
                + ":" + (seconds < 10 ? "0" : "") + seconds);
    }

    //elaspsed time in seconds
    public long getElapsedTimeSecs() {
        long elapsed;
        if (running) {
            elapsed = ((System.currentTimeMillis() - startTime) / 1000);
        } else {
            elapsed = ((stopTime - startTime) / 1000);
        }
        elapsed += this.timeInitial;
        return elapsed;
    }

    public void setElapseTimeSecs(long seconds) {
        this.timeInitial = seconds;
    }

    //sample usage
    public static void main(String[] args) {
        final StopWatch s = new StopWatch();
        s.setElapseTimeSecs(60 * 64);
        s.start();
        try {
            //code you want to time goes here
            Thread.sleep(12);
        } catch (InterruptedException ex) {
            Logger.getLogger(StopWatch.class.getName()).log(Level.SEVERE, null, ex);
        }
        s.stop();
        s.setElapseTimeSecs(60 * 65);

        JFrame fr = new JFrame("StopWatch");
        fr.setLayout(new GridLayout(3, 0));
        final JLabel strelapsed = new JLabel();
        JButton start = new JButton("Start");
        JButton stop = new JButton("Pause/Stop");


        fr.add(strelapsed);
        fr.add(start);
        fr.add(stop);

        fr.pack();
        fr.setVisible(true);
        fr.setLocationRelativeTo(null);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        start.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent me) {
                s.start();
            }
        });

        stop.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent me) {
                s.stop();
                s.setElapseTimeSecs(s.getElapsedTimeSecs());
                strelapsed.setText(s.getElapsedStringTime());
            }
        });

        strelapsed.setText(s.getElapsedStringTime());

        System.out.println("elapsed time in milliseconds: " + s.getElapsedTimeMilliSeconds());
        System.out.println("elapsed time in seconds: " + s.getElapsedTimeSecs());
        System.out.println("elapsed time: " + s.getElapsedStringTime());
    }

    class TimeElapsed {

        private long seconds = 0;
        private String elapsed = "00:00:00";

        public TimeElapsed(long seconds, String elapsed) {
            this.seconds = seconds;
            this.elapsed = elapsed;
        }

        public String getElapsed() {
            return elapsed;
        }

        public void setElapsed(String elapsed) {
            this.elapsed = elapsed;
        }

        public long getSeconds() {
            return seconds;
        }

        public void setSeconds(long seconds) {
            this.seconds = seconds;
        }
    }
}